package com.stx.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.stx.entity.Factory;
import com.stx.entity.MedicineForm;
import com.stx.jdbc.JdbcUtil;

public class FactoryDao {
	Connection con = null;
	PreparedStatement pst = null;

	// 展示公司名
	public List<Factory> showFactory() {

		List<Factory> F = new ArrayList<Factory>();
		try {
			con = JdbcUtil.getCon();
			pst = con.prepareStatement("select * from tbl_medicineFactory");
			ResultSet rs = pst.executeQuery();
			System.out.println(rs.next());
			while (rs.next()) {
				Factory f = new Factory();
				f.setMfactoryid(rs.getString("mfactoryid"));
				f.setMfactoryname(rs.getString("mfactoryname"));
				f.setMfactoryaddress(rs.getString("mfactoryaddress"));
				f.setMfactoryphone(rs.getString("mfactoryphone"));
				F.add(f);
			}

		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return F;
	}

	// 展示某一公司id
	public String findmFactoryid(String factoryname) {
		try {
			con = JdbcUtil.getCon();
			pst = con.prepareStatement("select mfactoryid from tbl_medicineFactory where mfactoryname = ?");
			pst.setString(1, factoryname);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				return rs.getString("mfactoryid");
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return null;
	}


	public boolean addFactory(String fname, String faddress, String fphone) {

		try {
			con = JdbcUtil.getCon();
			pst = con.prepareStatement("select mfactoryname from tbl_medicineFactory where mfactoryname = ?");
			pst.setString(1, fname);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				pst = con.prepareStatement("insert into tbl_medicineFactory values(seq_medicineFactory.nextval,?,?,?)");
				pst.setString(1, fname);
				pst.setString(2, faddress);
				pst.setString(3, fphone);
				return true;
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}

		return false;
	}
}
